package com.tencent.qcloud.timchat.model;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.ChatQABean;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.UrlUtils;
import com.tencent.TIMCustomElem;
import com.tencent.TIMMessage;
import com.tencent.qcloud.timchat.adapters.ChatAdapter;

/**
 * 自定义消息
 */
public class CustomMessage_ extends Message {

    public ChatQABean.QABean bean;
    private String TAG = getClass().getSimpleName();
    private String customType;
    CustomBean customBean;


    public CustomMessage_(TIMMessage message) {
        this.message = message;
        TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
        parse(elem.getData());
    }

    private void parse(byte[] data) {

        try {
            String str = new String(data, "UTF-8");


            customBean = GsonUtil.fromJson(str,CustomBean.class);
             customType =  customBean.customType;
            switch (customType) {

                case "auto_qa":
                    bean = GsonUtil.fromJson(customBean.customData,ChatQABean.QABean.class);
                    break;


            }

        } catch (Exception e) {
            Log.e(TAG, "parse json error");
            customType = "";
        }
    }

    /**
     * 显示消息
     *
     * @param viewHolder 界面样式
     * @param context    显示消息的上下文
     */
    @Override
    public void showMessage(final ChatAdapter.ViewHolder viewHolder, final Context context) {
        clearView(viewHolder);

        View v ;

        if (customType.equals("auto_qa")) {

                viewHolder.message_no_read_count.setVisibility(View.GONE);
                if (isSelf()) {
                    Glide.with(context).load(LoginInfo.getInstance(context).getUser().getUe_avatar()).error(R.drawable.empty_avatar).into(viewHolder.rightAvatar);
                } else {
                    Glide.with(context).load(viewHolder.toShowUrl).error(R.drawable.empty_avatar).into(viewHolder.leftAvatar);
                }

                TextView tv = new TextView(App.getContext());
                tv.setPadding(10, 10, 10, 10);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                tv.setTextColor(App.getContext().getResources().getColor(isSelf() ? R.color.white : R.color.black));
                String content = bean.title;
                UrlUtils.handleText(tv, content);
                getBubbleView(viewHolder).addView(tv);
                showStatus(viewHolder);

        }
     else {
            v = new TextView(context);
            ((TextView)v).setText("消息类型异常");
            getBubbleView(viewHolder).addView(v);
        }

        showStatus(viewHolder);
    }

    /**
     * 获取消息摘要
     */
    @Override
    public String getSummary() {
        String summary  ="";
        switch (customType) {
            case "produce":
                summary = "[产品]";
            break;
            case "plan":
                summary = "[计划书]";
                break;
            case "read":
                summary = "[微阅读]";
                break;
            case "evaluate":
                summary = "[评分结果]";
                break;
            case "invite":
                summary = "[评分邀请]";
                break;
            case "auto_qa":
              //  summary = bean.title;
                break;
        }

        return summary;
    }

    /**
     * 保存消息或消息文件
     */
    @Override
    public void save() {

    }

    public enum Type {
        TYPING,
        INVALID,
    }


}
